package com.zhengsr.latte.ec.ec.sign;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.TextInputEditText;
import android.util.Log;
import android.util.Patterns;
import android.view.View;

import com.alibaba.fastjson.JSON;
import com.zhengsr.ecloadinglib.view.EcBitmapLoadingView;
import com.zhengsr.latte.ec.R;
import com.zhengsr.latte.ec.R2;
import com.zhengsr.latte.ec.ec.database.SignBean;
import com.zhengsr.latte.net.RestClient;
import com.zhengsr.latte.net.callback.RestListenerAdapter;
import com.zhengsr.latte.utils.SprefUtils;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by zhengshaorui on 2018/4/22.
 */

public class SignUpDelegate extends BaseSignDelegate {
    private static final String TAG = "SignUpDelegate";

    @BindView(R2.id.sign_up_name)
    TextInputEditText mName;
    @BindView(R2.id.sign_up_email)
    TextInputEditText mEmail;
    @BindView(R2.id.sign_up_phone)
    TextInputEditText mPhone;
    @BindView(R2.id.sign_up_passwrod)
    TextInputEditText mPassword;
    @BindView(R2.id.sign_up_re_password)
    TextInputEditText mRePassword;




    private boolean checkForm(){
        final String name = mName.getText().toString();
        final String email = mEmail.getText().toString();
        final String phone = mPhone.getText().toString();
        final String password = mPassword.getText().toString();
        final String rePassword = mRePassword.getText().toString();

        boolean isPass = true;

        if (name.isEmpty()) {
            mName.setError("请输入姓名");
            isPass = false;
        } else {
            mName.setError(null);
        }

        if (email.isEmpty() || !Patterns.EMAIL_ADDRESS.matcher(email).matches()) {
            mEmail.setError("错误的邮箱格式");
            isPass = false;
        } else {
            mEmail.setError(null);
        }

        if (phone.isEmpty() || phone.length() != 11) {
            mPhone.setError("手机号码错误");
            isPass = false;
        } else {
            mPhone.setError(null);
        }

        if (password.isEmpty() || password.length() < 6) {
            mPassword.setError("请填写至少6位数密码");
            isPass = false;
        } else {
            mPassword.setError(null);
        }

        if (rePassword.isEmpty() || rePassword.length() < 6 || !(rePassword.equals(password))) {
            mRePassword.setError("密码不一致");
            isPass = false;
        } else {
            mRePassword.setError(null);
        }

        return isPass;
    }

    @OnClick(R2.id.sign_up_btn)
    void onClickSignUp(){
        //如果检测ok，则要把数据传递上服务器了

        if (checkForm()){
           // Log.d(TAG, "zsr --> 开始打印啦");
            EcBitmapLoadingView view = new EcBitmapLoadingView(getContext());
            RestClient client = new RestClient.Builder()
                    .setUrl("signup")
                    .setParams("name",mName.getText().toString())
                    .setParams("email",mEmail.getText().toString())
                    .setParams("phone",mPhone.getText().toString())
                    .setParams("password",mPassword.getText().toString())
                    .setParams("repassword",mRePassword.getText().toString())
                    .setListenerAdapter(new RestListenerAdapter() {
                        @Override
                        public void onSuccess(String reposonse) {
                            super.onSuccess(reposonse);
                            try {

                                String data = JSON.parseObject(reposonse).getString("data");
                                SignBean bean = JSON.parseObject(data,SignBean.class);
                                //保存到数据库
                                if(bean.saveOrUpdate()){
                                    startWithPop(new SignInDelegate());
                                    SprefUtils.saveSprefValue(SprefUtils.ISSIGNIN,true);
                                    //
                                    mISignCallback.onSignUpSuccess();
                                }
                            } catch (Exception e) {
                                e.printStackTrace();
                            }

                        }

                        @Override
                        public void onFailure(String msg) {
                            super.onFailure(msg);

                        }

                        @Override
                        public void onError(int code, String errormsg) {
                            super.onError(code, errormsg);
                            Log.d(TAG, "zsr --> onError: "+code+" "+errormsg);
                        }
                    })
                    .setDialog(getContext(),view)
                    .builder()
                    .post();
        }
    }

    @OnClick(R2.id.sign_in_link_tv)
    void onClickLinkSignIn(){
        //start(new SignInDelegate(),SINGLETASK);
        startWithPop(new SignInDelegate());
    }

    @Override
    public Object getLayout() {
        return R.layout.sign_up_layout;
    }

    @Override
    public void initView(@Nullable Bundle savedInstanceState, View view) {
    }
}
